//
//  HDHFileManager.m
//  BaiSiBuDeJIE
//
//  Created by 洪东恒 on 16/4/8.
//  Copyright © 2016年 洪东恒. All rights reserved.
//

#import "HDHFileManager.h"

@implementation HDHFileManager

+(NSString *)getDirectorySize:(NSString *)directoryPath
{
    //思路  获得文件夹路径
    //遍历所有文件夹的文件 累计文件的尺寸
    NSString *filePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    //    NSLog(@"%@",filePath);
    //获得文件管理者
    NSFileManager *manager = [NSFileManager defaultManager];
    //获得这个文件夹子类的所有文件路径
    NSArray *pathArray = [manager subpathsAtPath:directoryPath];
    
    NSInteger totalSize = 0;
    NSString *str = nil;
    
    //遍历所以文件  并且拼接路径
    for (NSString *subPath in pathArray) {
        
        //拼接路径名称
        NSString *path = [filePath stringByAppendingPathComponent:subPath];
        
        //当路径是文件夹 或者不存在  或者是隐藏文件的时候跳过这次循环
        BOOL isDirectory;
        BOOL isExist = [manager fileExistsAtPath:path isDirectory:&isDirectory];
        if (!isExist || isDirectory ) continue;
        
        if ([path containsString:@".DS"]) continue;
        
        // 指定路径获取这个路径的属性
        // attributesOfItemAtPath:只能获取文件属性
        NSDictionary *dict = [manager attributesOfItemAtPath:path error:nil];
        NSInteger size = [dict fileSize];
        totalSize += size;
        
    }
    
    //    NSLog(@"%ld",totalSize);
    //判断totalSize的大小
    if (totalSize > 1000 *1000) {
        
        CGFloat totalSizeF = totalSize /(1000.0 *1000.0);
        str = [NSString stringWithFormat:@"%.1fMB",totalSizeF];
        
    }else if (totalSize > 1000){
        
        CGFloat totalSizeF = totalSize /1000.0;
        str = [NSString stringWithFormat:@"%.1fKB",totalSizeF];
        
    }else if (totalSize > 0){
        
        str = [NSString stringWithFormat:@"%.1ldB",totalSize];
        
    }else if (totalSize <= 0){
        
        str = [NSString stringWithFormat:@"0.0M"];
        
    }
    return str;
}


+(void)removeDirectoryPath:(NSString *)directoryPath
{
    //获得文件管理者
    //获得该文件夹下的一级目录
    NSArray *file = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
    
    for (NSString *subPath in file) {
        
        //拼接路径
        NSString *subPaths = [directoryPath stringByAppendingPathComponent:subPath];
        //根据路径删除 文件夹
        [[NSFileManager defaultManager] removeItemAtPath:subPaths error:nil];
    }
    
}


@end
